package com.ztc.controller;

import com.alibaba.fastjson.JSON;
import com.ztc.beans.Glo;
import com.ztc.dao.GloDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/Glo")
public class GloController extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String method =  request.getParameter("method");
        System.out.println(method);
        if (method.equals("get1")){
            get1(request,response);
        }else if (method.equals("get")){
            get(request,response);
        }
    }

    private void get(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType( "text/text;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        GloDao gloDao1 = new GloDao();
        String id =request.getParameter("id");
        List<Glo> glist = gloDao1.getshi(id);
        String userJson = JSON.toJSONString(glist);
        try {
            response.getWriter().write(userJson);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void get1(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType( "text/text;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        GloDao gloDao1 = new GloDao();
        List<Glo> glist = gloDao1.getsheng();
        String userJson = JSON.toJSONString(glist);
        System.out.println(userJson);
        try {
            response.getWriter().write(userJson);
        } catch (IOException e) {
           e.printStackTrace();
        }
        
    }


}
